package net.chenlin.dp.modules.utils;

import net.chenlin.dp.modules.business.entity.vo.lineChart.AllTcpTotalVo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Yangsm
 * @ClassName: ArrayUtils
 * @Description: TODO
 * @date 2022年12月06日 16:54
 */
public class ArrayUtils {

    public static void closest_pair(int my_arr_1[], int my_arr_2[], int arr_1_len, int arr_2_len, int sum){
        int diff = Integer.MAX_VALUE;
        int result_l = 0, result_r = 0;
        int l = 0, r = arr_2_len-1;
        while (l<arr_1_len && r>=0){
            if (Math.abs(my_arr_1[l] + my_arr_2[r] - sum) < diff){
                result_l = l;
                result_r = r;
                diff = Math.abs(my_arr_1[l] + my_arr_2[r] - result_l);
            }
            if (my_arr_1[l] + my_arr_2[r] > result_l){
                r--;
            } else {
                l++;
            }
        }
        System.out.print("与两个数组匹配的最接近的对是 [" + my_arr_1[result_l] + ", " + my_arr_2[result_r] + "]");
    }

    public static void main(String args[]){
//        int my_arr_1[] = {56, 78, 99, 11};
//        int my_arr_2[] = {33, 12, 69, 87};
//        int arr_1_len = my_arr_1.length;
//        int arr_2_len = my_arr_2.length;
//        int val = 79;
//        closest_pair(my_arr_1, my_arr_2, arr_1_len, arr_2_len, val);
        //allTcpTotalVo vo = new allTcpTotalVo();
        //compareNum(1.0);
    }

    public static Integer compareNum(Double num, AllTcpTotalVo vo){

        BigDecimal numb = new BigDecimal(Double.toString(num));

        Double onePower = vo.getOnePower();

        Double twoPower = vo.getTwoPower();

        Double threePower = vo.getThreePower();


        List<Double> list = new ArrayList<>();

        if(onePower != null ){

            BigDecimal numb2 = new BigDecimal(Double.toString(onePower));
            double res = Math.abs(numb.subtract(numb2).doubleValue());
            list.add(res);
        }else{
            list.add(Double.MAX_VALUE);
        }

        if(twoPower != null ){
            BigDecimal numb2 = new BigDecimal(Double.toString(twoPower));
            double res = Math.abs(numb.subtract(numb2).doubleValue());
            list.add(res);
        }else{
            list.add(Double.MAX_VALUE);
        }

        if(threePower != null ){
            BigDecimal numb2 = new BigDecimal(Double.toString(threePower));
            double res = Math.abs(numb.subtract(numb2).doubleValue());
            list.add(res);
        }else{
            list.add(Double.MAX_VALUE);
        }
        Collections.sort(list);
        Double min = Collections.min(list);
        int index = list.indexOf(min);

        return index+1;
    }
}
